package com.fly.toolbox.component;

import com.fly.toolbox.config.ApplicationProperties;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextArea;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.Consumer;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import java.awt.*;


public class GenerateVersionLogsComponent {
    private final ApplicationProperties properties = ApplicationProperties.getInstance();

    private final JPanel formPanel;
    private final JBTextArea textArea = new JBTextArea(5, 50);
    private final JBTextField requirementNumberText = new JBTextField(30);
    private final JBTextField requirementBankText = new JBTextField(30);
    private final JBTextField requirementDescriptionText = new JBTextField(30);
    private final JBTextField changeRequestNumberText = new JBTextField(30);
    private final JButton submitButton = new JButton("确认");
    private final JButton parseButton = new JButton("\uD83D\uDD0D");
    private final JButton dropdownButton = new JButton("▼");
    private final ComboBox<String> versionComboBox = new ComboBox<>(properties.getVersions());

    public GenerateVersionLogsComponent() {

        versionComboBox.setEditable(true);

        // 创建表单面板并设置布局为GridBagLayout
        formPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = JBUI.insets(5);

        // 添加多行文本框
        gbc.gridx = 0; // 重置列索引
        gbc.gridy++;
        gbc.gridwidth = 2; // 设置组件横跨的列数
        formPanel.add(buildTextComponent(), gbc);

        gbc.gridwidth = 1; // 设置组件横跨的列数
        gbc.anchor = GridBagConstraints.WEST; // 设置文本对齐方式

        // 添加需求编号输入框
        gbc.gridx = 0;
        gbc.gridy++;
        formPanel.add(new JLabel("需求编号:"), gbc);
        gbc.gridx++;
        formPanel.add(requirementNumberText, gbc);

        // 添加需求引入行输入框
        gbc.gridx = 0;
        gbc.gridy++;
        formPanel.add(new JLabel("需求引入行:"), gbc);
        gbc.gridx++;
        formPanel.add(requirementBankText, gbc);

        // 添加需求描述输入框
        gbc.gridx = 0;
        gbc.gridy++;
        formPanel.add(new JLabel("需求描述:"), gbc);
        gbc.gridx++;
        formPanel.add(requirementDescriptionText, gbc);

        // 添加修改单编号输入框
        gbc.gridx = 0;
        gbc.gridy++;
        formPanel.add(new JLabel("修改单编号:"), gbc);
        gbc.gridx++;
        formPanel.add(changeRequestNumberText, gbc);

        // 添加标签和选择框
        gbc.gridx = 0;
        gbc.gridy++;
        formPanel.add(new JLabel("任务版本:"), gbc);
        gbc.gridx++;
        formPanel.add(versionComboBox, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2; // 设置组件横跨的列数
        gbc.anchor = GridBagConstraints.EAST; // 设置按钮对齐方式
        formPanel.add(submitButton, gbc);

    }

    public JPanel getPanel() {
        return formPanel;
    }

    public JComponent getPreferredFocusedComponent() {
        return textArea;
    }

    public JButton getSubmitButton() {
        return submitButton;
    }

    public JButton getParseButton() {
        return parseButton;
    }

    public JButton getDropdownButton() {
        return dropdownButton;
    }

    public void setRequirementNumberText(String newText) {
        requirementNumberText.setText(newText);
    }

    public String getRequirementNumberText() {
        return requirementNumberText.getText();
    }

    public String getVersionComboBoxValue() {
        return (String) versionComboBox.getSelectedItem();
    }

    public void setVersionComboBoxValue(String newValue) {
        versionComboBox.setSelectedItem(newValue);
    }

    public String getRequirementBankText() {
        return requirementNumberText.getText();
    }

    public void setRequirementBankText(String newText) {
        requirementBankText.setText(newText);
    }

    public String getRequirementDescriptionText() {
        return requirementDescriptionText.getText();
    }

    public void setRequirementDescriptionText(String newText) {
        requirementDescriptionText.setText(newText);
    }

    public String getChangeRequestNumberText() {
        return changeRequestNumberText.getText();
    }

    public void setChangeRequestNumberText(String newText) {
        changeRequestNumberText.setText(newText);
    }

    public String getTextArea() {
        return textArea.getText();
    }

    public void setTextArea(String newText) {
        textArea.setText(newText);
    }

    private JComponent buildTextComponent() {

        dropdownButton.setPreferredSize(new Dimension(20, 20));
        dropdownButton.setOpaque(false);

        parseButton.setPreferredSize(new Dimension(20, 20));
        parseButton.setOpaque(false);
        JPanel buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints btnGbc = new GridBagConstraints();
        btnGbc.gridx = 0;
        btnGbc.gridy = 0;
        btnGbc.insets = JBUI.insets(5);
        btnGbc.gridx = 0; // 重置列索引
        btnGbc.gridy++;
        buttonPanel.add(parseButton, btnGbc);

        btnGbc.gridx = 0; // 重置列索引
        btnGbc.gridy++;
        buttonPanel.add(dropdownButton, btnGbc);

        JPanel textAreaPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        textAreaPanel.add(new JScrollPane(textArea));
        textAreaPanel.add(buttonPanel);
        return textAreaPanel;
    }

    public void historyPopup(Consumer<String> c) {
        // 创建一个包含选项的数组
        String[] options = properties.getHistoryAsArray();

        // 创建一个JBList
        JBList<String> list = new JBList<>(options);

        // 创建一个包含JBList的JBScrollPane
        JBScrollPane scrollPane = new JBScrollPane(list);
        scrollPane.setPreferredSize(new Dimension(150, 100));

        // 创建一个JBPopup
        JBPopup popup = JBPopupFactory.getInstance()
                .createComponentPopupBuilder(scrollPane, null)
                .createPopup();

        // 为JBList添加选择事件
        list.addListSelectionListener(e2 -> {
            if (!e2.getValueIsAdjusting()) {
                String selectedValue = list.getSelectedValue();
                System.out.println("Selected: " + selectedValue);
                c.consume(selectedValue);
                popup.cancel();
            }
        });

        // 显示JBPopup
        popup.showUnderneathOf(dropdownButton);
    }

}
